suppressPackageStartupMessages({
  library(ggplot2)
  library(readr)
  library(ggthemes)
  library(dplyr)
  library(getopt)
})

# mask: 0=no argument
#       1=required argument
#       2=optional argument
spec <- matrix(c(
# long name  short name  mask  type          description(optional)
# ---------  ----------  ----  ------------  ---------------------
  'data'    , 'd',          1,  'character',  'data file',
  'output'  , 'o',          1,  'character',  'verbose output', 
  'help'    , 'h',          0,  'logical',    'show this help message'
), byrow=TRUE, ncol=5)
opt <- getopt(spec);
if (!is.null(opt$help)) {
    cat(getopt(spec, usage=TRUE))
    q();
}

x <- read_csv(opt$data, col_names = TRUE)



# bin the plddt
x$conflvl <- cut(x$best_pLDDT, breaks=c(0, 50, 70, 90, 100),
                 right=TRUE,
                 labels=c('<50', '50-70', '70-90', '>90'))

labels <- x %>%
  group_by(conflvl) %>%
  summarize(n=n())
labels$label <- sprintf("%3i / %3i", labels$n, nrow(x))
labels$pos <- c(40, 60, 80, 95)


p <- ggplot(x, aes(y=best_pLDDT)) +
  geom_density(size=1) +
  geom_hline(yintercept=c(50, 70, 90), linetype=3) +
  geom_text(aes(y=pos, label=label, x=0.045), data=labels) +
  scale_y_continuous(breaks=c(50, 70, 90)) +
  labs(x='Density', y='Best mean pLDDT of each OPG') +
  theme_tufte(18)

ggsave(opt$output, p)
